package c.j.a.d.b.e;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.util.SparseArray;
import c.j.a.d.b.d.b;
import c.j.a.d.b.d.g;
import c.j.a.d.b.d.j;
import c.j.a.d.b.j.q;
import c.j.a.d.b.n.a;
import com.ss.android.socialbase.downloader.g.c;
import com.ss.android.socialbase.downloader.impls.DownloadHandleService;
import java.util.ArrayList;
import java.util.List;

/* compiled from: DownloadProcessDispatcher */
public class d {
    public static volatile d d;
    public volatile SparseArray<Boolean> a = new SparseArray();
    public Handler b = new Handler(Looper.getMainLooper());
    public volatile List<g> c = new ArrayList();

    public static d b() {
        if (d == null) {
            synchronized (d.class) {
                d = new d();
            }
        }
        return d;
    }

    public int a(int i) {
        if (a.M() || !q.a(true).g()) {
            return e(i);
        }
        return q.a(true).M(i);
    }

    public final List<c> c(List<c> list, List<c> list2, SparseArray<c> sparseArray) {
        ArrayList arrayList = new ArrayList();
        if (list != null) {
            for (c cVar : list) {
                if (cVar != null && sparseArray.get(cVar.g0()) == null) {
                    sparseArray.put(cVar.g0(), cVar);
                }
            }
        }
        if (list2 != null) {
            for (c cVar2 : list2) {
                if (cVar2 != null && sparseArray.get(cVar2.g0()) == null) {
                    sparseArray.put(cVar2.g0(), cVar2);
                }
            }
        }
        for (int i = 0; i < sparseArray.size(); i++) {
            arrayList.add((c) sparseArray.get(sparseArray.keyAt(i)));
        }
        return arrayList;
    }

    public void d(int i, boolean z) {
        boolean z2;
        g(i, z);
        if (!a.M() && q.a(true).g()) {
            q.a(true).p(i, z);
        }
        Context context = b.a;
        synchronized (b.class) {
            z2 = b.B;
        }
        if (!z2 && !a.M() && !a.r()) {
            try {
                Intent intent = new Intent(b.a(), DownloadHandleService.class);
                intent.setAction("com.ss.android.downloader.action.PROCESS_NOTIFY");
                intent.putExtra("extra_download_id", i);
                b.a().startService(intent);
            } catch (Throwable th) {
                th.printStackTrace();
            }
        }
    }

    public synchronized int e(int i) {
        if (this.a.get(i) == null) {
            return -1;
        }
        return ((Boolean) this.a.get(i)).booleanValue();
    }

    public void f(int i, b bVar, c.j.a.d.b.b.g gVar, boolean z) {
        p h = h(i);
        if (h != null) {
            h.s(i, bVar.hashCode(), bVar, gVar, z);
        }
    }

    public synchronized void g(int i, boolean z) {
        this.a.put(i, z ? Boolean.TRUE : Boolean.FALSE);
    }

    public p h(int i) {
        boolean z = true;
        if (a(i) != 1 || a.M()) {
            z = false;
        }
        return q.a(z);
    }

    public j i(int i) {
        p h = h(i);
        if (h == null) {
            return null;
        }
        return h.Q(i);
    }
}
